
********* This program generates the network data with survey outcomes used in the regression analysis *********
*** NOTE that this is part 2 of the program generating the network data for analysis. One should run part 1 of the program, run the code in R, and then run this program

clear all
set more off, permanently
cd "$localdir\Data"
global output "$localdir\Output"





***************************************************** "FIRST STAGE" CONSTRUCTION ******************************************************
*** Generate analysis dataset with survey respondent outcomes and first-degree network links

use "Survey Members.dta", clear
g V1 = _n
save "Survey Members code.dta", replace

* Combine i-j network columns across years
use "network_pairs_FS 2010", clear
g year = 2010

append using "network_pairs_FS 2011"
replace year = 2011 if year==.

append using "network_pairs_FS 2012"
replace year = 2012 if year==.

append using "network_pairs_FS 2013"
replace year = 2013 if year==.

* Restrict attention to the people form whom we have political outcomes
merge m:1 V1 using "Survey Members code.dta"
keep if _merge==3
drop V1 _merge
rename V2 unique_network
order unique unique_network year

save "First stage", replace


* Create panel survey structure: main outcomes of interest and unique identifier (note: survey questions numbers differ across years)

use survey2010_us.dta, clear
* Rename to make clear the variable year
foreach x of varlist q19n1_us q39_us q40_us q41_us q42_us q43_us q44_us q45_us q46_us {
  drop if `x'==.
  rename `x' `x'_10
}
g year = 2010
save "S_2010.dta", replace

use survey2011_us.dta, clear
foreach x of varlist q11n1_us q35_us q36n1_us q40n1_us q41_us q43_us /*q46_us*/ q47_us q48_us q49_us { // q46 only asked to new participants
  drop if `x'==.
  rename `x' `x'_11
}
rename q46_us q46_us_11
g year = 2011
save "S_2011.dta", replace

use survey2012_us.dta, clear
foreach x of varlist q15n1_us q32_us q33n1_us q39_us q42_us q43_us q44_us q45_us q46_us q47_us {
  drop if `x'==.
  rename `x' `x'_12
}
g year = 2012
save "S_2012.dta", replace

use survey2013_us.dta, clear
foreach x of varlist q13n1_us q30n1_us q39n1_us q40_us q49_us /*q55_us*/ q56_us q57_us q58_us { // q55 only asked to new participants
  drop if `x'==.
  rename `x' `x'_13
}
rename q55_us q55_us_13
g year = 2013
save "S_2013.dta", replace

* Append all the survey data years together
use "S_2010.dta", clear
append using "S_2011.dta"
append using "S_2012.dta"
append using "S_2013.dta"

* Merge in the variable "unique", our unique identifier
destring pnr, replace
merge m:1 pnr using "Unique ID (pnr).dta"
keep if _merge==3
drop pnr _merge
order unique year

* Create variables to use in panel format
rename q19n1_us_10 prob_unemployed
foreach x of varlist q11n1_us_11 q15n1_us_12 q13n1_us_13  {
  replace prob_unemployed = `x' if prob_unemployed==.
  drop `x'
}
rename q39_us_10 econ_init 
foreach x of varlist q35_us_11 q32_us_12 {
  replace econ_init = `x' if econ_init==.
  drop `x'
}

rename q40_us_10 preferred_econ_policy

rename q41_us_10 govt_more_supp_unempl
foreach x of varlist q41_us_11 q39_us_12 q40_us_13  {
  replace govt_more_supp_unempl = `x' if govt_more_supp_unempl==.
  drop `x'
}
rename q42_us_10 imm_benefits
foreach x of varlist q43_us_11 q42_us_12 q49_us_13  {
  replace imm_benefits = `x' if imm_benefits==.
  drop `x'
}
rename q43_us_10 vote_last
foreach x of varlist q46_us_11 q44_us_12 q55_us_13  {
  replace vote_last = `x' if vote_last==.
  drop `x'
}
rename q44_us_10 vote_intent
foreach x of varlist q47_us_11 q45_us_12 q56_us_13  {
  replace vote_intent = `x' if vote_intent==.
  drop `x'
}
rename q46_us_10 important_success
foreach x of varlist q49_us_11 q47_us_12 q58_us_13  {
  replace important_success = `x' if important_success==.
  drop `x'
}
rename q45_us_10 govt_help
foreach x of varlist q48_us_11 q46_us_12 q57_us_13  {
  replace govt_help = `x' if govt_help==.
  drop `x'
}

g unemp_rate_estimate = q36n1_us_11
drop q36n1_us_11
foreach x of varlist q33n1_us_12 q30n1_us_13  {
  replace unemp_rate_estimate = `x' if unemp_rate_estimate==.
  drop `x'
}
g unemp_rate_estimate_next = q40n1_us_11
drop q40n1_us_11
foreach x of varlist q39n1_us_13  {
  replace unemp_rate_estimate_next = `x' if unemp_rate_estimate_next==.
  drop `x'
}

* Coding up specific variables to use in the analysis
g more_ui = govt_more_supp_unempl==1 if govt_more_supp_unempl!=.

g not_enough = econ_init==3 if econ_init!=.

g hard_work = important_success==1 if important_success!=.

g govt_more_help = (govt_help==1 | govt_help==2)

alpha prob_unemployed more_ui hard_work govt_help, std g(u_concern) reverse(hard_work govt_help)
alpha prob_unemployed more_ui hard_work govt_help if year<=2012, std g(u_concern_2010_2012) reverse(hard_work govt_help)

g further_decr_imm_benefits = imm_benefits==2 if imm_benefits!=.
g lower_imm_benefits = imm_benefits==2 | imm_benefits==3 if imm_benefits!=.
g raise_imm_benefits = imm_benefits==1 if imm_benefits!=.

g vote_govt = 0
replace vote_govt = 1 if vote_intent==1 & year>2011 | vote_intent==2 & year>2011 | vote_intent==4 & year>2011
replace vote_govt = 1 if vote_intent==3 & year<=2011 | vote_intent==6 & year<=2011
replace vote_govt = . if vote_intent==.

g vote_left = vote_intent==1 | vote_intent==2 | vote_intent==4 | vote_intent==8
replace vote_left = . if vote_intent==.

g voted_left_2011 = vote_last==1 | vote_last==2 | vote_last==4 | vote_last==8 if year>=2012 & vote_last!=.

save "Merged Panel Survey.dta", replace


* Create datasets with the outcomes of interest for subsequent merging with reduced form (note: only use two outcomes per merging set of outcomes due to data constraints)
preserve
keep unique year u_concern u_concern_2010_2012
save "Unemployment Views Outcome Scale.dta", replace
restore

preserve
keep unique year econ_init preferred_econ_policy
save "Economic Policy Outcomes.dta", replace
restore

preserve
keep unique year more_ui prob_unemployed
save "Unemployment Outcomes.dta", replace
restore

preserve
keep unique year vote_intent vote_left
save "Voting Outcomes.dta", replace
restore

* Vote in 2011: Use 2012 and 2013 survey merged onto shocks in 2010
preserve
keep if year==2012 | year==2013
replace year = 2011
keep if vote_last!=.
keep unique year voted_left_2011 vote_last
save "Vote 2011 Outcome.dta", replace
restore

preserve
keep unique year imm_benefits lower_imm_benefits
save "Immigration Policy Outcomes.dta", replace
restore

preserve
keep unique year govt_more_help hard_work
save "Ideology Outcomes.dta", replace
restore

preserve
keep unique year unemp_rate_estimate unemp_rate_estimate_next
save "Unemployment Perceptions.dta", replace
restore

* Make year correction for vote in 2011 in "merged panel survey" data: Use 2012 and 2013 survey merged onto shocks in 2010
drop voted_left_2011 vote_last
merge 1:1 unique year using "Vote 2011 Outcome.dta"
drop _merge // Keep all - matched and unmatched
save "Merged Panel Survey.dta", replace

* Merge in the survey outcomes by year (many merge==1 because not all 11000 panel participants exist in all years (only 6000 per year))

use "First stage", clear

merge m:1 unique year using "Merged Panel Survey.dta"
drop if _merge==2
drop _merge

save "First stage", replace


* Define the shocks

*use "registerdata0812_us.dta", clear
use "registerdata19802012_us.dta", clear
keep if year>=2009 & year<=2012
* As usual, merge in our unique identifier number
merge m:1 pnr using "Unique ID (pnr).dta"
keep if _merge==3
drop pnr _merge
egen parish_id = group(muni parish)
save "All.dta", replace

* Subset to potential shocks to connections (ie "_network")
keep year unique muni parish parish_id grossunemp_enter  
foreach x of varlist unique muni parish parish_id grossunemp_enter {
  rename `x' `x'_network
}
save "Individual shocks", replace

* Merge in local identifiers and own shocks (ie i level outcomes and locations)

use "All.dta", clear
keep unique year muni parish parish_id grossunemp_enter ginc_change_pct ginc_change
save "Identifiers.dta", replace

use "First stage", clear

* Lag years in order to merge with previous year's economic shocks (because survey happens at the beginning of each year)
replace year = year - 1

merge m:1 unique year using "Identifiers.dta"
drop if _merge==2
drop _merge


* Merge in the shocks to i's connections (j)

merge m:1 unique_network year using "Individual shocks.dta"
drop if _merge==2
drop _merge

replace year = year + 1

* Identify cases of within parish connections

g same_parish = muni==muni_network & parish==parish_network
g same_muni = muni==muni_network

bysort unique year : g network_size = _N

egen both = group(parish_id year)

save "First stage basic", replace // Before sample restrictions

keep if same_parish==0
drop if parish_id_network==.

save "First stage", replace














***************************************************** "REDUCED FORM" CONSTRUCTION ******************************************************
*** Generate analysis dataset with survey respondent outcomes and second-degree network links


* Recover the unique identifers in the RF for each year
forval y=2010/2013 {
use "instrument_pairs_RF `y'", clear
* Keep only i's that we have survey outcomes for
merge m:1 V1 using "Survey Members code.dta"
keep if _merge==3
drop V1 _merge
rename V2 unique_network

g year = `y'
order unique unique_network year
save "instrument_pairs_RF `y'", replace
}

* Merge in parish id of instrument (denoted "_network" for merging purposes, for now)
use "Individual shocks.dta", clear
keep unique_network year muni_network parish_network parish_id_network
save "network identifiers.dta", replace

foreach t in 2010 2011 2012 2013 {
  use "instrument_pairs_RF `t'", clear
  * Lag years in order to merge with previous year's parish (because survey happens at the beginning of each year)
  replace year = year - 1
  merge m:1 unique_network year using "network identifiers.dta", keepusing(muni_network parish_network parish_id_network)
  drop if _merge==2
  keep unique unique_network muni_network parish_network parish_id_network year
  replace year = year + 1
  save "instrument_pairs_RF `t'", replace
  keep unique parish_id_network year
  duplicates drop
  save "instrument_parishes_`t'.dta", replace
}

use "instrument_parishes_2010.dta", clear
append using "instrument_parishes_2011.dta"
append using "instrument_parishes_2012.dta"
append using "instrument_parishes_2013.dta"

drop year
duplicates drop

save "instrument parishes.dta", replace


* Recover the unique identifers in the FS
use "network_pairs_FS 2010", clear
g year = 2010

append using "network_pairs_FS 2011"
replace year = 2011 if year==.

append using "network_pairs_FS 2012"
replace year = 2012 if year==.

append using "network_pairs_FS 2013"
replace year = 2013 if year==.

merge m:1 V1 using "Survey Members code.dta"
keep if _merge==3
drop V1 _merge
rename V2 unique_network
order unique unique_network year

* Merge in parish id of instrument (denoted "_network" for merging purposes, for now)
* Lag years in order to merge with previous year's parish (because survey happens at the beginning of each year)
replace year = year - 1
merge m:1 unique_network year using "Individual shocks.dta", keepusing(parish_id_network)
drop if _merge==2
keep unique parish_id_network

duplicates drop

* Reshape to map each survey member to variables for each of the parishes within their network
bysort unique: g order = _n

reshape wide parish_id_network, i(unique) j(order)

save "network parishes.dta", replace


* Merge reshaped file into instrument set
use "instrument parishes.dta", clear

merge m:1 unique using "network parishes.dta"
keep if _merge==3
drop _merge


* REMOVE ANY INSTRUMENT THAT COINCIDES WITH THE PARISH OF ANY MEMBER OF THE SURVEYED PERSON'S NETWORK
foreach x of varlist parish_id_network1-parish_id_network1263 {
  drop if parish_id_network==`x'
}
keep unique parish_id_network

save "Reduced form.dta", replace


* Merge back in individual instrument identifiers
merge 1:m unique parish_id_network using "instrument_pairs_RF 2010", keepusing(unique_network)
keep if _merge==3
drop _merge
g year = 2010
save "Reduced form 2010.dta", replace

use "Reduced form.dta", clear
merge 1:m unique parish_id_network using "instrument_pairs_RF 2011", keepusing(unique_network)
keep if _merge==3
drop _merge
g year = 2011
save "Reduced form 2011.dta", replace

use "Reduced form.dta", clear
merge 1:m unique parish_id_network using "instrument_pairs_RF 2012", keepusing(unique_network)
keep if _merge==3
drop _merge
g year = 2012
save "Reduced form 2012.dta", replace

use "Reduced form.dta", clear
merge 1:m unique parish_id_network using "instrument_pairs_RF 2013", keepusing(unique_network)
keep if _merge==3
drop _merge
g year = 2013
save "Reduced form 2013.dta", replace

append using "Reduced form 2012.dta"
append using "Reduced form 2011.dta"
append using "Reduced form 2010.dta"

* Merge in local identifiers and own shocks from the previous year
replace year = year - 1

merge m:1 unique year using "Identifiers.dta"
keep if _merge==3
drop _merge


* Focus on main variables
drop if parish_id_network==parish_id
drop parish
drop if parish_id==. | parish_id_network==.

replace year = year + 1

save "Reduced form ALL.dta", replace



***** SHOCKS FROM LAST YEAR

*** Merge in the instrument shocks from the previous year

replace year = year - 1

* Individual unemployment shock on main unemployment outcome (note that not all merge because not all panel survey participants participate every year)
drop ginc_change ginc_change_pct
merge m:1 unique_network year using "Individual shocks"
keep if _merge==3
drop _merge

replace year = year + 1

bysort unique year : g network_size = _N

egen both = group(parish_id year)

save "Reduced form for outcome merge.dta", replace



*** Merge in different outcomes

* Individual unemployment shock on vote intention (note that not all merge because not all panel survey participants participate every year)
use "Reduced form for outcome merge.dta", clear

merge m:1 unique year using "Voting Outcomes.dta"
keep if _merge==3
drop _merge

drop if vote_intent==. & vote_left==.

save "Voting RF Individual Unemployment Shock.dta", replace


* Individual unemployment shock on vote choice in 2011 (note that not all merge because not all panel survey participants participate every year)
use "Reduced form for outcome merge.dta", clear

merge m:1 unique year using "Vote 2011 Outcome.dta"
keep if _merge==3
drop _merge

drop if voted_left_2011==. & vote_last==.

save "Voted RF Individual Unemployment Shock.dta", replace


* Individual unemployment shock on unemployment outcomes (note that not all merge because not all panel survey participants participate every year)
use "Reduced form for outcome merge.dta", clear

merge m:1 unique year using "Unemployment Outcomes.dta"
keep if _merge==3
drop _merge

drop if more_ui==. & prob_unemployed==.

save "RF Unemployment Outcomes.dta", replace


* Individual unemployment shock on economic policy outcomes (note that not all merge because not all panel survey participants participate every year)
use "Reduced form for outcome merge.dta", clear

merge m:1 unique year using "Economic Policy Outcomes.dta"
keep if _merge==3
drop _merge

drop if econ_init==. & preferred_econ_policy==.

save "RF Economic Policy Outcomes.dta", replace


* Individual unemployment shock on ideology outcomes (note that not all merge because not all panel survey participants participate every year)
use "Reduced form for outcome merge.dta", clear

merge m:1 unique year using "Ideology Outcomes.dta"
keep if _merge==3
drop _merge

drop if govt_more_help==. & hard_work==.

save "RF Ideology Outcomes.dta", replace


* Individual unemployment shock on unemployment rate perceptions (note that not all merge because not all panel survey participants participate every year)
use "Reduced form for outcome merge.dta", clear

merge m:1 unique year using "Unemployment Perceptions.dta"
keep if _merge==3
drop _merge

drop if unemp_rate_estimate==.

save "RF Unemployment Perceptions.dta", replace














***************************************************** PREPARE INDUSTRY, OCCUPATION, EDUCATION, AND AGE INFO USED IN THE REGRESSIONS ******************************************************


*** Industry
use "industry_20082012.dta", clear

*** Create industry-year identifiers (with separate categories for those out of the labor force (0), missing (99) and unspecified (999))
g industry_code_1_digit = .
replace industry_code_1_digit = ind_1 if industry_code_1_digit==.
replace industry_code_1_digit = pastind_1 if industry_code_1_digit==.
replace industry_code_1_digit = futureind_1 if industry_code_1_digit==.
replace industry_code_1_digit = 99 if mis==1 & mis_work==0 & industry_code_1_digit==.
replace industry_code_1_digit = 999 if industry_newfmt=="999999" & industry_code_1_digit==.
replace industry_code_1_digit = 0 if industry_code_1_digit==.

egen ind_year_1 = group(year industry_code_1_digit)

g industry_code_2_digit = .
replace industry_code_2_digit = ind_2 if industry_code_2_digit==.
replace industry_code_2_digit = pastind_2 if industry_code_2_digit==.
replace industry_code_2_digit = futureind_2 if industry_code_2_digit==.
replace industry_code_2_digit = 99 if mis==1 & mis_work==0 & industry_code_2_digit==.
replace industry_code_2_digit = 999 if industry_newfmt=="999999" & industry_code_2_digit==.
replace industry_code_2_digit = 0 if industry_code_2_digit==.

egen ind_year_2 = group(year industry_code_2_digit)

g industry_code_3_digit = .
replace industry_code_3_digit = ind_3 if industry_code_3_digit==.
replace industry_code_3_digit = pastind_3 if industry_code_3_digit==.
replace industry_code_3_digit = futureind_3 if industry_code_3_digit==.
replace industry_code_3_digit = 99 if mis==1 & mis_work==0 & industry_code_3_digit==.
replace industry_code_3_digit = 999 if industry_newfmt=="999999" & industry_code_3_digit==.
replace industry_code_3_digit = 0 if industry_code_3_digit==.

egen ind_year_3 = group(year industry_code_3_digit)

*** Merge in the unique identifier
merge m:1 pnr using "Unique ID (pnr).dta"
keep if _merge==3

keep year unique ind_year_1 ind_year_2 ind_year_3 industry_code_1_digit industry_code_2_digit industry_code_3_digit

* Move forward a year to reflect last year's industry (more accurate since the survey is in January)
replace year = year + 1
drop if year<2010

* Final data, individual
save "Industry codes.dta", replace

* Data network

foreach var of varlist unique ind_year_1 ind_year_2 ind_year_3 industry_code_1_digit industry_code_2_digit industry_code_3_digit {
rename `var' `var'_network
}

* Final data network
save "Industry codes (network).dta", replace



*** Occupation
use "occupation_education_20082012.dta", clear

g occupation_code_1_digit = .
replace occupation_code_1_digit = ocp1 if occupation_code_1_digit==.
replace occupation_code_1_digit = pastocp1 if occupation_code_1_digit==.
replace occupation_code_1_digit = futureocp1 if occupation_code_1_digit==.
replace occupation_code_1_digit = 999 if occupation_code_1_digit==.

egen ocp_year_1 = group(year occupation_code_1_digit)

*** Merge in the unique identifier
merge m:1 pnr using "Unique ID (pnr).dta"
keep if _merge==3

keep year unique ocp_year_1 occupation_code_1_digit

* Move forward a year to reflect last year's industry (more accurate since the survey is in January)
replace year = year + 1
drop if year<2010

* Final data, individual
save "Occupation codes.dta", replace

* Data network

foreach var of varlist unique ocp_year_1 occupation_code_1_digit {
rename `var' `var'_network
}

* Final data network
save "Occupation codes (network).dta", replace



*** Education
use "occupation_education_20082012.dta", clear

g educ_code_1_digit = .
replace educ_code_1_digit = educ1 if educ_code_1_digit==.
replace educ_code_1_digit = 999 if educ_code_1_digit==.

egen educ_year_1 = group(year educ_code_1_digit)

g educ_code_2_digit = .
replace educ_code_2_digit = educ2 if educ_code_2_digit==.
replace educ_code_2_digit = 999 if educ_code_2_digit==.

egen educ_year_2 = group(year educ_code_2_digit)

g educ_code_3_digit = .
replace educ_code_3_digit = educ3 if educ_code_3_digit==.
replace educ_code_3_digit = 999 if educ_code_3_digit==.

egen educ_year_3 = group(year educ_code_3_digit)

*** Merge in the unique identifier
merge m:1 pnr using "Unique ID (pnr).dta"
keep if _merge==3

keep year unique educ_year_1 educ_year_2 educ_year_3 educ_code_1_digit educ_code_2_digit educ_code_3_digit

* Move forward a year to reflect last year's industry (more accurate since the survey is in January)
replace year = year + 1
drop if year<2010

* Final data, individual
save "Education codes.dta", replace

* Data network
foreach var of varlist unique educ_year_1 educ_year_2 educ_year_3 educ_code_1_digit educ_code_2_digit educ_code_3_digit {
rename `var' `var'_network
}

* Final data network
save "Education codes (network).dta", replace



*** Age
use "age_20082012.dta", clear

*** Merge in the unique identifier
merge m:1 pnr using "Unique ID (pnr).dta"
keep if _merge==3

keep year unique age

* Move forward a year to reflect last year's industry (more accurate since the survey is in January)
replace year = year + 1
drop if year<2010

* Final data, individual
save "Age.dta", replace

* Data network
foreach var of varlist unique age {
rename `var' `var'_network
}

* Final data network
save "Age (network).dta", replace








********************************************** FINAL REGRESSION DATA FOR ANALYSES OF SURVEY RESPONDENT OUTCOMES ON SECOND-DEGREE NETWORK SHOCKS *************************************************





***************************************************** UNEMPLOYMENT OUTCOMES - PROBABILITY ******************************************************

use "RF Unemployment Outcomes.dta", clear

keep unique unique_network  prob_unemployed grossunemp_enter_network grossunemp_enter year both muni muni_network parish_id_network

* Sample restrictions
drop if muni==muni_network

merge m:1 unique_network year using "Age (network).dta"
drop if _merge==2
drop if age_network>65 | age_network<20
drop _merge age_network

* Get industry, occupation, and education for fixed effects
* Preferred specification: industry 2d-occupation 1d-year, education 1d-year, and parish-year fixed effects
merge m:1 unique year using "Industry codes.dta", keepusing(industry_code_2_digit)
replace industry_code_2_digit = 999 if _merge==1 
drop if _merge==2
drop _merge

merge m:1 unique year using "Occupation codes.dta", keepusing(occupation_code_1_digit)
replace occupation_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

merge m:1 unique year using "Education codes.dta", keepusing(educ_code_1_digit)
replace educ_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

* FE groups
egen ind2_year = group(industry_code_2_digit year)
egen ocp_year = group(occupation_code_1_digit year)
egen educ_year = group(educ_code_1_digit year)
egen ind2_ocp_year = group(industry_code_2_digit occupation_code_1_digit year)

keep if prob_unemployed!=. & grossunemp_enter_network!=.

save "Unemployment Outcomes RF for Analysis - Last Year.dta", replace






***************************************************** UNEMPLOYMENT OUTCOMES - UI POLICY ******************************************************

use "RF Unemployment Outcomes.dta", clear

keep unique unique_network more_ui grossunemp_enter_network grossunemp_enter year both muni muni_network parish_id_network

* Sample restrictions
drop if muni==muni_network

merge m:1 unique_network year using "Age (network).dta"
drop if _merge==2
drop if age_network>65 | age_network<20
drop _merge age_network

* Get industry, occupation, and education for fixed effects
* Preferred specification: industry 2d-occupation 1d-year, education 1d-year, and parish-year fixed effects
merge m:1 unique year using "Industry codes.dta", keepusing(industry_code_2_digit)
replace industry_code_2_digit = 999 if _merge==1 
drop if _merge==2
drop _merge

merge m:1 unique year using "Occupation codes.dta", keepusing(occupation_code_1_digit)
replace occupation_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

merge m:1 unique year using "Education codes.dta", keepusing(educ_code_1_digit)
replace educ_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

* FE groups
egen ind2_year = group(industry_code_2_digit year)
egen ocp_year = group(occupation_code_1_digit year)
egen educ_year = group(educ_code_1_digit year)
egen ind2_ocp_year = group(industry_code_2_digit occupation_code_1_digit year)

keep if more_ui!=. & grossunemp_enter_network!=.

save "UI Outcomes RF for Analysis - Last Year.dta", replace








***************************************************** ECONOMIC POLICY ******************************************************

use "RF Economic Policy Outcomes.dta", clear

keep unique unique_network econ_init preferred_econ_policy grossunemp_enter_network grossunemp_enter year both muni muni_network parish_id_network

g gov_solution = preferred_econ_policy==2 | preferred_econ_policy==5 if preferred_econ_policy!=. 
drop preferred_econ_policy

g not_enough = econ_init==3 if econ_init!=.
drop econ_init

* Sample restrictions
drop if muni==muni_network

merge m:1 unique_network year using "Age (network).dta"
drop if _merge==2
drop if age_network>65 | age_network<20
drop _merge age_network

* Get industry, occupation, and education for fixed effects
* Preferred specification: industry 2d-occupation 1d-year, education 1d-year, and parish-year fixed effects
merge m:1 unique year using "Industry codes.dta", keepusing(industry_code_2_digit)
replace industry_code_2_digit = 999 if _merge==1 
drop if _merge==2
drop _merge

merge m:1 unique year using "Occupation codes.dta", keepusing(occupation_code_1_digit)
replace occupation_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

merge m:1 unique year using "Education codes.dta", keepusing(educ_code_1_digit)
replace educ_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

* FE groups
egen ind2_year = group(industry_code_2_digit year)
egen ocp_year = group(occupation_code_1_digit year)
egen educ_year = group(educ_code_1_digit year)
egen ind2_ocp_year = group(industry_code_2_digit occupation_code_1_digit year)

keep if gov_solution!=. & grossunemp_enter_network!=.

save "Economic Policy Outcomes RF for Analysis - Last Year.dta", replace





***************************************************** VOTED LEFT ******************************************************

use "Voted RF Individual Unemployment Shock.dta", clear

keep unique unique_network voted_left_2011 vote_last grossunemp_enter_network grossunemp_enter year both muni muni_network parish_id_network

* Sample restrictions
drop if muni==muni_network

merge m:1 unique_network year using "Age (network).dta"
drop if _merge==2
drop if age_network>65 | age_network<20
drop _merge age_network

* Get industry, occupation, and education for fixed effects
* Preferred specification: industry 2d-occupation 1d-year, education 1d-year, and parish-year fixed effects
merge m:1 unique year using "Industry codes.dta", keepusing(industry_code_2_digit)
replace industry_code_2_digit = 999 if _merge==1 
drop if _merge==2
drop _merge

merge m:1 unique year using "Occupation codes.dta", keepusing(occupation_code_1_digit)
replace occupation_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

merge m:1 unique year using "Education codes.dta", keepusing(educ_code_1_digit)
replace educ_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

* FE groups
egen ind2_year = group(industry_code_2_digit year)
egen ocp_year = group(occupation_code_1_digit year)
egen educ_year = group(educ_code_1_digit year)
egen ind2_ocp_year = group(industry_code_2_digit occupation_code_1_digit year)

keep if voted_left_2011!=. & grossunemp_enter_network!=.

save "Voted RF for Analysis - Last Year.dta", replace






***************************************************** VOTING LEFT (INTENTION) ******************************************************

use "Voting RF Individual Unemployment Shock.dta", clear

keep unique unique_network vote_* grossunemp_enter_network grossunemp_enter year both muni muni_network parish_id_network

* Sample restrictions
drop if muni==muni_network

merge m:1 unique_network year using "Age (network).dta"
drop if _merge==2
drop if age_network>65 | age_network<20
drop _merge age_network

* Get industry, occupation, and education for fixed effects
* Preferred specification: industry 2d-occupation 1d-year, education 1d-year, and parish-year fixed effects
merge m:1 unique year using "Industry codes.dta", keepusing(industry_code_2_digit)
replace industry_code_2_digit = 999 if _merge==1 
drop if _merge==2
drop _merge

merge m:1 unique year using "Occupation codes.dta", keepusing(occupation_code_1_digit)
replace occupation_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

merge m:1 unique year using "Education codes.dta", keepusing(educ_code_1_digit)
replace educ_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

* FE groups
egen ind2_year = group(industry_code_2_digit year)
egen ocp_year = group(occupation_code_1_digit year)
egen educ_year = group(educ_code_1_digit year)
egen ind2_ocp_year = group(industry_code_2_digit occupation_code_1_digit year)

keep if vote_left!=. & grossunemp_enter_network!=.

save "Voting RF for Analysis - Last Year.dta", replace





***************************************************** IDEOLOGY OUTCOMES ******************************************************

use "RF Ideology Outcomes.dta", clear

keep unique unique_network govt_more_help hard_work grossunemp_enter_network grossunemp_enter year both muni muni_network parish_id_network

* Sample restrictions
drop if muni==muni_network

merge m:1 unique_network year using "Age (network).dta"
drop if _merge==2
drop if age_network>65 | age_network<20
drop _merge age_network

* Get industry, occupation, and education for fixed effects
* Preferred specification: industry 2d-occupation 1d-year, education 1d-year, and parish-year fixed effects
merge m:1 unique year using "Industry codes.dta", keepusing(industry_code_2_digit)
replace industry_code_2_digit = 999 if _merge==1 
drop if _merge==2
drop _merge

merge m:1 unique year using "Occupation codes.dta", keepusing(occupation_code_1_digit)
replace occupation_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

merge m:1 unique year using "Education codes.dta", keepusing(educ_code_1_digit)
replace educ_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

* FE groups
egen ind2_year = group(industry_code_2_digit year)
egen ocp_year = group(occupation_code_1_digit year)
egen educ_year = group(educ_code_1_digit year)
egen ind2_ocp_year = group(industry_code_2_digit occupation_code_1_digit year)

keep if govt_more_help!=. & grossunemp_enter_network!=.

save "Ideology RF for Analysis - Last Year.dta", replace






***************************************************** ASSESSMENT OF NATIONAL UNEMPLOYMENT RATE ******************************************************

use "RF Unemployment Perceptions.dta", clear

keep unique unique_network unemp_rate_estimate grossunemp_enter_network grossunemp_enter year both muni muni_network parish_id_network

* Sample restrictions
drop if muni==muni_network

merge m:1 unique_network year using "Age (network).dta"
drop if _merge==2
drop if age_network>65 | age_network<20
drop _merge age_network

* Get industry, occupation, and education for fixed effects
* Preferred specification: industry 2d-occupation 1d-year, education 1d-year, and parish-year fixed effects
merge m:1 unique year using "Industry codes.dta", keepusing(industry_code_2_digit)
replace industry_code_2_digit = 999 if _merge==1 
drop if _merge==2
drop _merge

merge m:1 unique year using "Occupation codes.dta", keepusing(occupation_code_1_digit)
replace occupation_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

merge m:1 unique year using "Education codes.dta", keepusing(educ_code_1_digit)
replace educ_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

* FE groups
egen ind2_year = group(industry_code_2_digit year)
egen ocp_year = group(occupation_code_1_digit year)
egen educ_year = group(educ_code_1_digit year)
egen ind2_ocp_year = group(industry_code_2_digit occupation_code_1_digit year)

keep if unemp_rate_estimate!=. & grossunemp_enter_network!=.

save "National Unemployment RF for Analysis - Last Year.dta", replace





***************************************************** FORECAST OF NATIONAL UNEMPLOYMENT RATE ******************************************************

use "RF Unemployment Perceptions.dta", clear

keep unique unique_network unemp_rate_estimate_next grossunemp_enter_network grossunemp_enter year both muni muni_network parish_id_network

* Sample restrictions
drop if muni==muni_network

merge m:1 unique_network year using "Age (network).dta"
drop if _merge==2
drop if age_network>65 | age_network<20
drop _merge age_network

* Get industry, occupation, and education for fixed effects
* Preferred specification: industry 2d-occupation 1d-year, education 1d-year, and parish-year fixed effects
merge m:1 unique year using "Industry codes.dta", keepusing(industry_code_2_digit)
replace industry_code_2_digit = 999 if _merge==1 
drop if _merge==2
drop _merge

merge m:1 unique year using "Occupation codes.dta", keepusing(occupation_code_1_digit)
replace occupation_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

merge m:1 unique year using "Education codes.dta", keepusing(educ_code_1_digit)
replace educ_code_1_digit = 999 if _merge==1 
drop if _merge==2
drop _merge 

* FE groups
egen ind2_year = group(industry_code_2_digit year)
egen ocp_year = group(occupation_code_1_digit year)
egen educ_year = group(educ_code_1_digit year)
egen ind2_ocp_year = group(industry_code_2_digit occupation_code_1_digit year)

keep if unemp_rate_estimate_next!=. & grossunemp_enter_network!=.

save "National Unemployment Forecast RF for Analysis - Last Year.dta", replace




















